package com.eascs.finance.admin.server.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.finance.admin.common.entity.ArticleEntity;
import com.eascs.finance.admin.common.entity.ArticleItemEntity;
import com.eascs.finance.admin.common.tool.PinYin2Abbreviation;
import com.eascs.finance.admin.common.vo.ArticleQueryVO;
import com.eascs.finance.admin.server.manager.IArticleManager;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Repository
public class ArticleManagerImpl extends BaseDao implements IArticleManager {

	private final Logger logger = LoggerFactory.getLogger(ArticleManagerImpl.class);

	@Override
	public String addArticle(ArticleEntity articleEntity) {
		User user = UserHolder.getUser();
		articleEntity.setUpdateTime(new Date());
		articleEntity.setCreateTime(new Date());
		if (user != null) {
			articleEntity.setUpdater(user.getCname());
			articleEntity.setCreator(user.getCname());
		}

		if (StringUtil.isEmpty(articleEntity.getArticleUrl())) {
			articleEntity.setArticleUrl(PinYin2Abbreviation.cn2py(articleEntity.getArticleTitle()));
		}

		if (StringUtil.isEmpty(articleEntity.getFrontDisplayTime())) {
			articleEntity.setFrontDisplayTime(new Date());
		}
		articleEntity.setStatus(1);
		articleEntity.setIsLock(0);
		boolean flag = this.saveEntity(articleEntity);
		if (flag == true) {
			return articleEntity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateArticle(ArticleEntity articleEntity) {
		User user = UserHolder.getUser();
		articleEntity.setUpdateTime(new Date());
		if (user != null) {
			articleEntity.setUpdater(user.getCname());
		}

		if (StringUtil.isEmpty(articleEntity.getArticleUrl())) {
			articleEntity.setArticleTitle(PinYin2Abbreviation.cn2py(articleEntity.getArticleTitle()));
		}
		return this.updateEntity(articleEntity);
	}

	@Override
	public PageResult<Object[]> findArticleInfo(ArticleQueryVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());

		StringBuilder sql = new StringBuilder();
		sql.append(
				"select article.id,article.article_title,aitem.item_name,article.article_keywords ,article.publish_time,article.read_num,case when is_top=0 then '否' else '是' end,article.publisher,item.item_name as status from article article"
						+ " left join datadic_item item on article.status=item.item_code and dd_no=2  "
						+ " left join article_item aitem on article.item_id=aitem.id" + " where article.is_lock=0 ");

		// 文章标题
		if (StringUtil.isNotBlank(vo.getArticleTitle())) {
			sql.append(" and article.article_title like ?");
			sqlPageQuery.addParam("%" + vo.getArticleTitle() + "%");

		}
		// 文章状态
		if (vo.getStatus() != null) {
			sql.append(" and article.status= ?");
			sqlPageQuery.addParam(vo.getStatus());
		}
		// 栏目类型
		if (StringUtils.isNotEmpty(vo.getItemId())) {
			sql.append(" and article.item_id= ?");
			sqlPageQuery.addParam(vo.getItemId());
		}
		sql.append(" order by is_top desc ,article.create_time desc");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findByPage(sqlPageQuery);
		return page;
	}

	@Override
	public boolean deleteArticle(String id) {

		return this.excuteSql("update article set is_lock=1 where id=? ", id);
	}

	@Override
	public boolean updateArticleStatus(String id, int status) {
		User user = UserHolder.getUser();
		String sql = "update article set status=? where id=? ";
		if (status == 2) {

			sql = "update article set status=?, publisher=?, publish_time=? where id=?  ";
			return this.excuteSql(sql, status, user.getCname(), new Date(), id);
		}
		return this.excuteSql(sql, status, id);
	}

	@Override
	public ArticleEntity findArticleById(String id) {

		return this.getById(ArticleEntity.class, id);
	}

	@Override
	public PageResult<ArticleEntity> findFrontArticleInfo(ArticleQueryVO vo) {
		SqlPageQuery<ArticleEntity> sqlPageQuery = new SqlPageQuery<>(ArticleEntity.class, vo.getPageSize(),
				vo.getShowPage());

		StringBuilder sql = new StringBuilder();
		sql.append(
				"select article.id,article.article_title,aitem.item_name,article.article_keywords ,article.publish_time,article.read_num,case when is_top=0 then '否' else '是' end,article.publisher from article article"
						+ " left join article_item aitem on article.item_id=aitem.id"
						+ " where article.is_lock=0 and article.status=2");

		// 栏目类型
		if (StringUtils.isNotEmpty(vo.getItemId())) {
			sql.append(" and article.item_id= ?");
			sqlPageQuery.addParam(vo.getItemId());
		}

		sql.append(" order by item_id desc,is_top desc ,article.create_time desc");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<ArticleEntity> page = this.findByPage(sqlPageQuery);
		return page;
	}

	@Override
	public List<ArticleItemEntity> getArticleItemList() {

		return this.findEntitys("select id ,item_name from article_item  where is_lock=0 and status=2",
				ArticleItemEntity.class);
	}

	@Override
	public boolean updateReadNum(String id) {
		return this.excuteSql("update article set read_num=read_num+1 where id=? ", id);
	}

}
